<?php

namespace app\job;

use app\api\model\BaseConfig as BaseConfigModel;
use app\api\model\MemberTransaction;
use app\api\model\MemberTransaction as MemberTransactionModel;
use app\common\model\UserMoneyLog as UserMoneyLogModel;
use think\queue\Job;
use think\cache\driver\Redis;
use think\facade\Config;
use \Predis\Client;
use think\facade\Db;

class Task
{
    public function fire(Job $job, $data)
    {
        $isJobDone = $this->doJob($data);
        if ($isJobDone) {
            $job->delete();
        } else {
            // 消息队列执行失败
            // 获取消息队列已经重试了几遍
            $attempts = $job->attempts();
            if ($attempts == 0 || $attempts == 1) {
                // 重新发布，参数 delay 是延时发布的时间
                $job->release(2);
            }
        }


        //print_r($data);
/*
        // 具体执行业务
        $isJobDone = $this->doJob($data);

        if ($isJobDone) {
            // 消息队列执行成功，删除队列，否则会一直执行
            $job->delete();
        } else {
            // 消息队列执行失败
            // 获取消息队列已经重试了几遍
            $attempts = $job->attempts();
            if ($attempts == 0 || $attempts == 1) {
                // 重新发布，参数 delay 是延时发布的时间
                $job->release(2);
            }
        }
        print_r($data);die;

        $adc_transaction = BaseConfigModel::config_data('adc_transaction');

        if($adc_transaction <= $data['money'])
        {
            $member_entrust_id = Db::name('member_entrust')->where(['id'=>$data['id'],'entrust_state'=>2])->find();

            if($member_entrust_id)  $job->delete();

            $member_entrust  = Db::name('member_entrust')->where(['id'=>$data['id']])->update(['entrust_state'=>2]);

            if($member_entrust)
            {
                //添加一条交易记录
                MemberTransaction::add_transaction($data['mid'],$data['currency'],$data['money'],$data['num'],$data['transaction_state']);
                //记录ADC数量变化
                UserMoneyLogModel::change($data['mid'],'adc',$data['money'],'','','交易增加'.$data['money'].'adc');
            }
            echo '成功'.$data['id'];
            $job->delete();
        }
        echo  '失败'.$data['id'];*/
    }

    // 消息队列执行失败后会自动执行该方法
    public function failed($data)
    {

    }

    // 消息队列执行方法
    public function doJob($data)
    {
        Db::startTrans();
        try {

            $v['mid'] = $data['buy_data']['mid'];
            $v['money'] = $data['buy_data']['money'];
            $v['id'] = $data['buy_data']['id'];

            $price = $data['price'];

            $val['mid'] = $data['sell_data']['mid'];
            $val['money'] = $data['sell_data']['money'];
            $val['id'] = $data['sell_data']['id'];

            MemberTransactionModel::add_transaction($v['mid'],1,$v['money'],$price,1,0,2,$v['id'],$val['mid']);

            MemberTransactionModel::add_transaction($val['mid'],2,$val['money'],$price,2,transaction_commission($price,$val['money']),2,$val['id'],$v['mid']);

            UserMoneyLogModel::change($v['mid'],'adc',$price,'Transaction buy','','交易增加'.$price.'adc');

            $usdt_obtain = commission("$price",$val['money']);

            UserMoneyLogModel::change($val['mid'],'usdt',-$usdt_obtain,'Transaction buy','','交易减少'.$usdt_obtain.'usdt');

            Db::commit();
            echo '成功';
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            echo '失败';
            return false;
        }

    }
}
